﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using INSE6260.OnlineBanking.Model.Accounts;
using INSE6260.OnlineBanking.Model.Clients;
using INSE6260.OnlineBanking.Model.EmploymentType;
using INSE6260.OnlineBanking.Model.OccupationType;
using INSE6260.OnlineBanking.Web.MVC.Helpers;

namespace INSE6260.OnlineBanking.Web.MVC.Controllers
{
    public class AccountController : Controller
    {
        private readonly IAccountRepository _accounService;
        private readonly IClientRepository _clientService;
        public AccountController(IAccountRepository accountRepository, IClientRepository clientRepository)
        {
            _accounService = accountRepository;
            _clientService = clientRepository;
        }
        public ActionResult Create()
        {

            var client = _clientService.FindClient(UserProvider.ActiveClient.ClientId);

            var accountInfo = new AccountInfo { Email = client.Email, Address = client.Address, Telephone = client.Telephone,OccupationTypeId = client.OccupationTypeId, EmploymentTypeId = client.EmploymentTypeId};

            var accountTypeList = _accounService.GetNoneExistAccountType(UserProvider.ActiveClient.ClientId);
            if (accountTypeList.Count == 0)
            {
                ViewBag.Message = "Sorry! There is no other type of account available for you";
                return View("NoAccount");
            }
            ViewBag.AccountDesc = accountTypeList;

            var occupationType = new List<OccupationType> { new OccupationType { Name = "Student", Id = 1 }, new OccupationType { Name = "Government Employee", Id = 2 } };
            ViewBag.OccupationType = occupationType;

            var employmentType = new List<EmploymentType> { new EmploymentType { Name = "Fulltime", Id = 1 }, new EmploymentType { Name = "Part Time", Id = 2 } };
            ViewBag.EmploymentType = employmentType;

            return View("Create", accountInfo);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(AccountInfo accountInfo)
        {
            if (ModelState.IsValid)
            {
                var client = _clientService.GetClientByClientId(UserProvider.ActiveClient.ClientId);
                client.Email = accountInfo.Email;
                client.Telephone = accountInfo.Telephone;
                client.Address = accountInfo.Address;
                client.OccupationTypeId = accountInfo.OccupationTypeId;
                client.EmploymentTypeId = accountInfo.EmploymentTypeId;


                Account acc = new CheckingAccount();
                var prefix=0;
                if (accountInfo.AccountDesc == "Checking")
                {
                    acc = new CheckingAccount();
                    prefix = 99;
                    acc.AvailableBalance = 0;
                    acc.Balance = 0;

                }
                if (accountInfo.AccountDesc== "Saving")
                {
                    acc = new SavingAccount();
                    prefix = 80;
                    acc.AvailableBalance = 0;
                    acc.Balance = 0;

                }
                if (accountInfo.AccountDesc == "Credit")
                {
                    acc = new CreditAccount();
                    prefix = 12;
                    acc.AvailableBalance = 1500;
                    acc.Balance = 1500;

                }
                var ascii = System.Text.Encoding.ASCII;
                acc.ClientId = client.Id;
                acc.AccountNo = prefix.ToString(CultureInfo.InvariantCulture) + ascii.GetBytes(UserProvider.ActiveClient.LastName.Substring(0, 2))[0] +"-"+ ascii.GetBytes(UserProvider.ActiveClient.FirstName.Substring(0, 2))[1] + "/" + client.Id.ToString(CultureInfo.InvariantCulture);
                acc.OpenDate = DateTime.Now;
                acc.WPD = 1000;
                acc.WPT = 10000;
                acc.Status = false;

                _accounService.Add(acc);
                _clientService.UpdateProfile(client);
                return RedirectToAction("Index","Home");
            }
            ViewBag.Message = "The Account was NOT created seccessfully";
            return View(accountInfo);
        }
    }
}
